package com.tiantianyuegou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.activity.MyEvaluateActivity;
import com.tiantianyuegou.activity.MyOrderDetailsActivity;
import com.tiantianyuegou.model.GoodsInfo;
import com.tiantianyuegou.model.ShopInfoModel;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/11.
 */
public class ShopNameAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private Context context;
    private String[] text_shop_name;
    private String[] text_prompt;
    private String[] text_piece;

    private LayoutInflater inflater;
    private GoodsInfoAdapter goodsInfoAdapter;

    private List<ShopInfoModel> data;

    public ShopNameAdapter() {

    }

    public ShopNameAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ShopInfoModel shopInfoModel = new ShopInfoModel();
            List<GoodsInfo> models = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                GoodsInfo model = new GoodsInfo();
                model.setText_goods_name("一个水杯");
                model.setText_goods_info("这就是一个水杯");
                model.setText_goods_money("10.00");
                model.setText_goods_num("8");
                model.setText_goods_state("未付款");
                models.add(model);
            }
            shopInfoModel.setShop_name("水" + i);
            shopInfoModel.setShop_prompt("待评价");
            shopInfoModel.setGoodsInfos(models);
            shopInfoModel.setShop_piece("共16件");
            shopInfoModel.setShop_money_num("160.00");
            data.add(shopInfoModel);
        }
    }

    public ShopNameAdapter(Context context, String[] text_shop_name, String[] text_prompt, String[] text_piece) {
        this.context = context;
        this.text_shop_name = text_shop_name;
        this.text_prompt = text_prompt;
        this.text_piece = text_piece;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        Holder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_my_order_shop_name, null);
            holder = new Holder();
            holder.text_shop_name = (TextView) view.findViewById(R.id.text_shop_name);
            holder.text_prompt = (TextView) view.findViewById(R.id.text_prompt);
            holder.text_piece = (TextView) view.findViewById(R.id.text_piece);
            holder.linear_goods = (LinearLayout) view.findViewById(R.id.linear_goods);
            holder.btn_left = (Button) view.findViewById(R.id.btn_left);
            holder.btn_right = (Button) view.findViewById(R.id.btn_right);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.text_shop_name.setText(data.get(i).getShop_name());
        holder.text_prompt.setText(data.get(i).getShop_prompt());
        holder.text_piece.setText(data.get(i).getShop_piece());
        holder.linear_goods.removeAllViews();

        for (int j = 0; j < data.get(i).getGoodsInfos().size(); j++) {
            final GoodsInfo goods = data.get(i).getGoodsInfos().get(j);

            View view1 = inflater.inflate(R.layout.item_list_my_goods, holder.linear_goods, false);
            ((ImageView) view1.findViewById(R.id.img_goods_picture)).setImageResource(R.mipmap.beizi);
            ((TextView)view1.findViewById(R.id.text_goods_name)).setText(goods.getText_goods_name());
            ((TextView)view1.findViewById(R.id.text_goods_info)).setText(goods.getText_goods_info());
            ((TextView)view1.findViewById(R.id.text_goods_money)).setText(goods.getText_goods_money());
            ((TextView)view1.findViewById(R.id.text_goods_num)).setText(goods.getText_goods_num());
            ((TextView)view1.findViewById(R.id.text_goods_state)).setText(goods.getText_goods_state());
            holder.linear_goods.addView(view1);
        }



        holder.btn_left.setText(R.string.order_btn_fukuan);
        holder.btn_right.setText(R.string.order_btn_pingjiadingdan);

        holder.btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent();
                i.setClass(context, MyOrderDetailsActivity.class);
                context.startActivity(i);
            }
        });

        holder.btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent r = new Intent();
                r.setClass(context, MyEvaluateActivity.class);
                context.startActivity(r);
            }
        });

//        ArrayList<JSONArray> test=test();
//        goodsInfoAdapter=new GoodsInfoAdapter(context,test);
//        holder.list_goods_name.setAdapter(goodsInfoAdapter);
//
//        holder.list_goods_name.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    public static class Holder {
        TextView text_shop_name, text_prompt, text_piece,text_goods_name,text_goods_info,text_goods_money,text_goods_num,text_goods_state,btn_tuikuan;
        ListView list_goods_name;
        Button btn_left, btn_right;
        LinearLayout linear_goods;

        ImageView img_goods_picture;
    }

    private ArrayList<JSONArray> test() {
        ArrayList<JSONArray> test = null;
        JSONArray arr = null;
        try {
            test = new ArrayList<JSONArray>();
            arr = new JSONArray();
            JSONObject object = new JSONObject();
            object.put("name", "a");
            object.put("info", "a_info");
            object.put("money", "a_money");
            object.put("num", "a_num");
            object.put("state", "a_state");
            arr.put(0, object);
            test.set(0, arr);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return test;
    }
}
